package com.sdwz.user.api;

import com.sdwz.commonutil.Result;
import com.sdwz.serviceutil.utils.JwtUtil;

import com.sdwz.user.entity.UserInfo;
import com.sdwz.user.entity.vo.AuthVo;
import com.sdwz.user.entity.vo.RegisterVo;
import com.sdwz.user.service.BorrowCardInfoService;
import com.sdwz.user.service.StuInfoService;
import com.sdwz.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserApiController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    StuInfoService stuInfoService;

    @Autowired
    BorrowCardInfoService borrowCardInfoService;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @PostMapping("/login")
    public Result login(@RequestBody UserInfo userInfo){
        Map<String,String> result = userInfoService.login(userInfo);
        System.out.println(result);
        return Result.ok(result);
    }

    /**
     * 注册账户
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo){
        userInfoService.register(registerVo);
        return Result.ok();
    }

    /**
     * 获取个人信息
     * @param request
     * @return
     */
    @GetMapping("/auth/ownInfo")
    public Result getUserselveInfo(HttpServletRequest request){
        //头文件获取token
        String token = request.getHeader("token");
        Long userId = JwtUtil.getUserId(token);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

    /**
     * 修改个人信息
     * @param request
     * @param userInfo
     * @return
     */
    @PostMapping("/auth/ownInfo/update/{code}")
    public Result updateOwnInfo(HttpServletRequest request,
                                @PathVariable
                                String code,
                                @RequestBody
                                UserInfo userInfo){
        //验证码校验
        System.out.println(code);
        String code1 = redisTemplate.opsForValue().get(userInfo.getPhone());
        System.out.println(code1);
        System.out.println(userInfo);
        if (!code1.equals(code)){
            return Result.fail().message("验证码错误");
        }
        //头文件获取token
        String token = request.getHeader("token");
        String userId = String.valueOf(JwtUtil.getUserId(token));
        userInfo.setId(userId);
        boolean flag = userInfoService.updateById(userInfo);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail().message("修改失败");
        }
    }

    /**
     * 申请认证,添加未激活的借阅卡
     * @param request
     * @param authVo
     * @return
     */
    @PostMapping("/auth/certificate")
    public Result certificate(HttpServletRequest request,
                                 @RequestBody
                                 AuthVo authVo){
        String token = request.getHeader("token");
        String userId = String.valueOf(JwtUtil.getUserId(token));
        userInfoService.certificate(userId,authVo);

        return Result.ok();

    }

    /**
     * 查看自己的实名信息
     * @param request
     * @return
     */
    @GetMapping("/auth/getAuthInfo")
    public Result getCertificateInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = String.valueOf(JwtUtil.getUserId(token));

        AuthVo authInfo = userInfoService.getCertificateInfo(userId);
        return Result.ok(authInfo);
    }

    /**
     * 删除自己的认证消息
     * @param request
     * @return
     */
    @DeleteMapping("/auth/delAuthInfo")
    public Result delCertificateInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = String.valueOf(JwtUtil.getUserId(token));

        userInfoService.delCertificateInfo(userId);
        return Result.ok();
    }



}
